package com.four.hospital.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.four.common.utils.R;
import com.four.hospital.dao.*;
import com.four.hospital.entity.*;
import com.four.hospital.service.DoctorService;
import com.four.hospital.vo.DoctorAndUserVo;
import com.four.hospital.vo.DoctorVo;
import com.four.hospital.vo.DoctorVo3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;

/**
 * @Author:luojie
 * @Date: 2022/01/05/18:39
 * @Description
 */
@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorDao doctorDao;

    @Autowired
    private DoctorAndUserDao doctorAndUserDao;

    @Autowired
    private HospitalDao hospitalDao;

    @Autowired
    private DepartDao departDao;

    @Autowired
    private DiseaseDao diseaseDao;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 根据医院id，科室id，等级id查询医生信息
     * @param page
     * @param size
     * @param vo
     * @return
     */
    @Override
    public IPage<Doctor> doctorList(Integer page, Integer size, DoctorVo vo) {
        Page<Doctor> doctorPage=new Page<>(page,size);
        return doctorDao.doctorList(doctorPage,vo);
    }

    /**
     * 根据医生id查询患者
     * @param docId
     * @return
     */
    @Override
    public IPage<DoctorAndUserVo> doctorUser(Integer docId,Integer page,Integer size) {
        Page<DoctorAndUserVo> doctorAndUserVoPage=new Page<>(page,size);
        return doctorDao.doctorUser(doctorAndUserVoPage,docId);
    }

    /**
     * 根据医生id回显到数据
     *
     * @param docId
     * @return
     */
    @Override
    public Doctor findById(Integer docId) {
        return doctorDao.findById(docId);
    }

    /**
     * 根据医生id来修改审核状态
     *
     * @param docId
     * @param doctor
     * @return
     */
    @Override
    public void upDateDoctorById(Integer docId, Doctor doctor) {
        doctor.setDocId(docId);
        doctorDao.upDateDoctorById(doctor);
    }

    /**
     * 输入医生姓名查询信息
     * @param dosName
     * @return
     */  
    @Override
    public List<Doctor> getDoctor(String dosName) {
        return doctorDao.getDoctor(dosName);
    }

    /**
     * 根据科室id查询所有医生
     * @param depId
     * @return
     */
    @Override
    public List<DoctorVo3> getDoctorByDepId(Integer depId) {
        //查询所有医生信息
        List<DoctorVo3>  doctorVo3s=doctorDao.getDoctorByDepId(depId);
        //获取医生编号去查询医生和患者中间表，获取一生的服务人次
        for (DoctorVo3 doctorVo3 : doctorVo3s) {
            Integer peopleNums=doctorAndUserDao.getNum(doctorVo3.getDocId());
            doctorVo3.setPeopleNum(peopleNums);
        }
        return doctorVo3s;
    }

    /**
     * 根据选中的城市查询这个城市所有有的医生
     * @param areaId
     * @return
     */
    @Override
    public R getDoctorByAreaId(Integer areaId) {
        //先根据地址编号查询这个城市所有的医院
        List<Hospital> hospitals=hospitalDao.getHospitalAll(areaId);
        for (Hospital hospital : hospitals) {
            List<DoctorVo3> doctorVo3s=doctorAndUserDao.doctorAndUser(hospital.getHosId());
            //获取医生编号去查询医生和患者中间表，获取一生的服务人次
            for (DoctorVo3 doctorVo3 : doctorVo3s) {
                Integer peopleNum=doctorAndUserDao.getNum(doctorVo3.getDocId());
                doctorVo3.setPeopleNum(peopleNum);
            }
            return R.ok().put("doctorVo3",doctorVo3s);
        }
        return R.ok();
    }

    /**
     * 输入病症查询医生
     * @param diseName
     * @return
     */
    @Override
    public R getDoctorByDise(String diseName){
        //1.根据输入的病症去疾病表查询信息
        Disease disease=diseaseDao.getDiseByName(diseName);
        //增加改病症的查询次数
        int disenum=disease.getDiseNum()+1;
        diseaseDao.updateDiseNum(disease.getDiseId(),disenum);
        //根据id查询对应科室
        Depart depart=departDao.getDepartById(disease.getDepId());
        //根据科室id查询对应医生
        List<DoctorVo3> doctorVo3List=doctorDao.getDoctorByDepId(depart.getDepId());
        //获取医生编号去查询医生和患者中间表，获取一生的服务人次
        for (DoctorVo3 doctorVo3 : doctorVo3List) {
            Integer peopleNum=doctorAndUserDao.getNum(doctorVo3.getDocId());
            doctorVo3.setPeopleNum(peopleNum);
        }
        return R.ok().put("doctorVo3List",doctorVo3List);
    }

    /**
     * 设置个人信息
     * @param docName
     * @param docPhone
     * @param docId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDoctor(String docName, String docPhone, Integer docId) {
        Doctor doctor = doctorDao.getDoctorById(docId);
        doctor.setDocName(docName);
        doctor.setDocPhone(docPhone);
        return R.ok();
    }

    /**
     * 设置密码
     * @param oldPwd
     * @param newPwd
     * @param docId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDoctorPwd(String oldPwd, String newPwd, Integer docId) {
        Doctor doctor = doctorDao.getDoctorById(docId);
        if (!doctor.getDocPwd().equals(oldPwd)) {
            return R.error(500,"密码不正确");
        }else {
            doctor.setDocPwd(newPwd);
            return R.ok();
        }
    }
}
